package protocol;

import java.net.InetAddress;

public class User {

	private InetAddress ip;
	private int port;
	private String name;
	private long lastAnnouncement;

	public User(InetAddress ip, int port, String name) {
		this.ip = ip;
		this.port = port;
		this.name = name;
		this.lastAnnouncement = System.currentTimeMillis();
	}

	public boolean modify(InetAddress ip, int port, String name) {
		boolean change = !this.ip.equals(ip) || this.port != port || !this.name.equals(name);
		this.ip = ip;
		this.port = port;
		this.name = name;
		this.lastAnnouncement = System.currentTimeMillis();
		return change;
	}
	
	public long lastConnection(long now) {
		return now - lastAnnouncement;
	}

	public InetAddress getIp() {
		return ip;
	}

	public void setIp(InetAddress ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return name+" ("+ip.getHostAddress()+":"+port+")";
	}

}
